Inside Solaris logo
The Cobb Group This article is reprinted from the July 1996 issue of  Inside Solaris, a monthly publication of The Cobb Group.

Click for a FREE issue!


SCRIPTING TECHNIQUES

Conditionally executing shell scripts

You sometimes might want to run a shell script at a scheduled time to perform some administrative tasks, like making a backup. If you use crontab to schedule your scripts, you're just about set. The only problem we encounter with this technique is that while you normally want to execute the script, there are occasions when you don't. You could remove the appropriate crontab entry when you don't want to execute the script and then reenter it when you do. However, this method is tedious and errorprone. In this article, we'll show you a simpler technique: We'll let the script decide when to execute based on the presence of a particular file.

How does the shell decide whether to run?

First we need a method to allow the shell script to decide whether to run. It turns out that this is very easy to do. Figure A shows our demonstration shell script, named test. The highlighted lines show the part that checks for the file /locks/test. The heart of the trick is the -e filename clause in the condition section of the if statement. This clause is true if filename specifies a file that exists; otherwise the clause is false. The rest of your shell script remains unchanged.

In our demonstration, if the file /locks/test exists, the script prints a message to let you know why it terminated. Then it executes the exit statement to stop processing the script.

Figure A

#! /bin/ksh

# Test whether shell script is allowed to run

if [ -e /locks/test ]; then
  echo "/locks/test exists, script terminated"
  exit
fi

# Normal shell script operation starts here

echo "Hello, world!"

If this shell script finds the file /locks/test, it prints a warning and exits.


Create a directory of lock files

In order to make this technique manageable, you should keep your lock files in a centralized location. For our purposes, we simply created the /locks directory. Creating this directory gives you the ability to easily determine which shell scripts are locked out on your system. All you need to do is execute the command

$ ls /locks

and you'll see a list of the scripts that are currently locked out from use. Whenever you want prevent a shell script from operating, just execute the command

$ touch /locks/scriptname

where scriptname is the name of the script you want to lock out. Then, when you want to allow access to this script again, just remove the lock file with the rm command

$ rm /locks/scriptname

Figure B shows our demonstration script in action. First we run test normally, and then we lock it out by creating the file /locks/test and run test again. Finally, we delete the lock file and test runs normally again.

Figure B

Here's how the technique works: First test runs normally, we lock it out, and then we allow normal operation again.

Conclusion

This technique is useful whenever you want to prevent a certain script file from running. If you're scheduling a late night and don't want to be slowed down by a large finite element analysis job, you can turn off the script that runs it. Or perhaps your tape drive was removed for repair. If so, you can use this technique to turn off the backup script.

 

[The Cobb Group Home Page]

Copyright (c) 1996 The Cobb Group, a division of Ziff-Davis Publishing Company. All rights reserved. Reproduction in whole or in part in any form or medium without express written permission of Ziff-Davis Publishing Company is prohibited. The Cobb Group and The Cobb Group logo are trademarks of Ziff-Davis Publishing Company.

Questions? Comments?